import request from "@/router/axios";
import ActionTypes from "../const/action";

const API_KEYWORD_MAP = {
  /** @desc 拉取分类图标列表 */
  [ActionTypes.CATEGORY_ICON]: "classified",
  /** @desc 拉取轮播图列表 */
  [ActionTypes.PICTURE_MANAGE]: "slideShow",
  /** @desc 拉取商品一级分类列表 */
  [ActionTypes.GOODS_CATEGORY_FIRST]: "first",
  /** @desc 拉取商品二级分类列表 */
  [ActionTypes.GOODS_CATEGORY_SECOND]: "categorySon",
  /** @desc 拉取展会管理列表 */
  [ActionTypes.EXHIBITION_MANAGE]: "exhibition",
  /** @desc 拉取商品列表 */
  [ActionTypes.GOODS_MANAGE]: "product",
  /** @desc 拉取商户列表 */
  [ActionTypes.MERCHANT_LIST]: "merchant",
  /**@desc 拉取审核列表 */
  [ActionTypes.MERCHANT_AUDIT_LIST]: "merchant",
  /**@desc 评价列表 */
  [ActionTypes.MERCHANT_COMMENT_LIST]: "merchant",
  /**@desc 拉取员工列表 */
  [ActionTypes.PERSONNEL_MANAGE]: "staff",
  /**@desc 拉取账户管理列表 */
  [ActionTypes.ACCOUNT_AUTH]: "user",
  /**@desc 拉取问题管理列表 */
  [ActionTypes.REPAIR_QUESTION]: "question",
  /**@desc 拉取弹窗列表 */
  [ActionTypes.HOMEPAGE_POPUP]: "popup",
  /**@desc 佣金设置列表 */
  [ActionTypes.COMMISSION_MANAGE]: "commission",
  /**@desc 维修订单列表 */
  [ActionTypes.MAINTAIN_ORDER]: "maintainOrder",
  /**@desc 角色管理列表 */
  [ActionTypes.ROLE_MANAGE]: "role",
  /**@desc 订单 */
  [ActionTypes.ORDER_MANAGE]: "order",
  /**@desc 优惠券 */
  [ActionTypes.COUPON_MANAGE]: "order",
  /**@desc 后台用户 */
  [ActionTypes.USER_CORE_MANAGE]: "userCore",
  /**@desc 购买历史 */
  [ActionTypes.USER_CORE_HISTORY]: "userCore",
  /**@desc 提现列表 */
  [ActionTypes.WITHDRAW_MANAGE]: "withdraw",
};

/**
 * 拉取列表
 * @param {string} action
 * @param {any} data
 * @param {string} method
 */
export const SyncGetTableData = (action, data, method = "POST") => {
  const options = {
    url: `/admin/${API_KEYWORD_MAP[action] || "none"}/adminPage`,
    headers: {
      "Content-Type": "application/json;",
    },
    method,
    data,
  };

  if (action === ActionTypes.ACCOUNT_AUTH) {
    options.headers = {
      "Content-Type": "application/x-www-form-urlencoded;",
    };
    options.url = "/admin/user/findAdminUserByPage";
  }
  if (action === ActionTypes.ROLE_MANAGE) {
    options.headers = {
      "Content-Type": "application/x-www-form-urlencoded;",
    };
    options.url = "/admin/role/findAppRoleByPage";
  }
  if (action === ActionTypes.COUPON_MANAGE) {
    options.url = "/admin/order/couponManage";
  }
  if (action === ActionTypes.MERCHANT_LIST) {
    options.data.auditStatus = "PASS";
  }
  if (action === ActionTypes.MERCHANT_AUDIT_LIST) {
    options.data.auditStatus = "WAIT_AUDIT";
  }
  if (action === ActionTypes.USER_CORE_HISTORY) {
    options.url = "/admin/userCore/purchaseRecord";
  }
  if (action === ActionTypes.MERCHANT_COMMENT_LIST) {
    options.url = "/admin/order/merchantComment";
  }
  const checkError = checkPost(action, options.data);
  if (checkError) {
    return Promise.resolve({
      error: checkError,
    });
  }
  return request(options)
    .then((res) => ({
      data: res.data.data,
    }))
    .catch((error) => ({
      error,
    }));
};

/**
 * 拉取详情
 * @param {string} action
 * @param {number} id
 * @param {string} method
 */
export const SyncGetDetailData = (action, id, method = "POST") => {
  const options = {
    url: `/admin/${API_KEYWORD_MAP[action] || "none"}/detail`,
    headers: {
      "Content-Type": "application/x-www-form-urlencoded;",
    },
    method,
    data: {
      id,
    },
  };
  if (action === ActionTypes.ROLE_MANAGE) {
    options.url = "/admin/role/getRoleInfoByRoleId";
    options.data = {
      roleId: id,
    };
  }
  if (action === ActionTypes.ACCOUNT_AUTH) {
    options.url = "/admin/user/findAdminUserDetailByUserId";
    options.data = {
      userId: id,
    };
  }
  if (action === ActionTypes.COUPON_MANAGE) {
    options.url = "/admin/order/couponManageDetail";
  }
  if (action === ActionTypes.ORDER_MANAGE) {
    options.url = "/admin/order/adminDetail";
  }
  if (action === ActionTypes.MERCHANT_COMMENT_LIST) {
    options.url = "/admin/order/adminDetail";
  }
  return request(options)
    .then((res) => ({
      data: res.data.data,
    }))
    .catch((error) => ({
      error,
    }));
};

/**
 * 保存详情
 * @param {string} action
 * @param {any} data
 * @param {string} method
 */
export const PromiseSaveDetailData = (action, data, method = "POST") => {
  const options = {};
  options.url = `/admin/${API_KEYWORD_MAP[action] || "none"}/save`;
  options.method = method;
  options.data = data;
  options.headers = {
    "Content-Type": "application/json;",
  };

  if (action === ActionTypes.ROLE_MANAGE) {
    options.headers = {
      "Content-Type": "application/x-www-form-urlencoded;",
    };
    options.url = "/admin/role/saveAppRole";
  }
  if (action === ActionTypes.ACCOUNT_AUTH) {
    options.url = "/admin/user/saveAdminUser";
    options.headers = {
      "Content-Type": "application/x-www-form-urlencoded;",
    };
  }
  return request(options);
};

/**
 * 删除某条数据
 * @param {string} action
 * @param {number} id
 */
export const PromiseDeleteRow = (action, id) => {
  const options = {};
  options.url = `/admin/${API_KEYWORD_MAP[action] || "none"}/delete`;
  options.data = {
    id,
  };
  options.headers = {
    "Content-Type": "application/x-www-form-urlencoded;",
  };
  options.method = "POST";
  if (action === ActionTypes.ROLE_MANAGE) {
    options.url = "/admin/role/deleteRoleByRoleId";
    options.data = {
      roleId: id,
    };
  }
  if (action === ActionTypes.ACCOUNT_AUTH) {
    options.url = "/admin/user/deleteAdminUserByUserId";
    options.data = {
      userId: id,
    };
  }
  if (action === ActionTypes.MERCHANT_COMMENT_LIST) {
    options.url = "/admin/userComment/deleted";
  }
  return request(options);
};

function checkPost(action, data) {
  if (data.startTime && data.endTime) {
    if (new Date(data.startTime).getTime() > new Date(data.endTime).getTime()) {
      return "开始时间不能大于结束时间";
    }
  }

  return false;
}
